package org.cloudnote.controller;

import lombok.extern.slf4j.Slf4j;
import org.cloudnote.bean.Note;
import org.cloudnote.bean.NoteResult;
import org.cloudnote.bean.Share;
import org.cloudnote.service.NoteService;
import org.cloudnote.util.NoteUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/note")
@Slf4j
public class NoteController {

    @Autowired
    private NoteService noteService;


    @RequestMapping("/loadnotes.do")
    public NoteResult getByBookId(String bookId){
        log.info("loadnote执行{}",bookId);
        List<Note> notes = noteService.getByBookId(bookId);
        NoteResult result = new NoteResult();
        result.setStatus(0);
        result.setMsg("查询成功");
        result.setData(notes);
        return result;
    }
    @RequestMapping("/loadnote.do")
    public NoteResult getByNoteId(String noteId){
        Note note = noteService.getByNoteId(noteId);
        NoteResult result = new NoteResult();
        result.setStatus(0);
        result.setData(note);
        return result;
    }
    @RequestMapping("/save.do")
    public NoteResult save(Note note){
        NoteResult result = new NoteResult();
        noteService.save(note);
        result.setStatus(0);
        result.setData(note);
        result.setMsg("添加成功");
        return result;
    }

    @RequestMapping("/update.do")
    public NoteResult update(Note note){
        NoteResult result = new NoteResult();
        noteService.update(note);
        result.setData(note);
        result.setStatus(0);
        result.setMsg("修改成功");
        return result;
    }
    @RequestMapping("/recycle.do")
    public NoteResult recycle(String noteId){
        NoteResult result = new NoteResult();
        noteService.recycle(noteId);
        result.setStatus(0);
        result.setMsg("删除成功");
        return result;
    }

    @RequestMapping("/share.do")
    public NoteResult saveShare(String noteId){
        //先判断笔记是否分享过
        NoteResult result = new NoteResult();
        Share share = noteService.getShareByNoteId(noteId);
        Note note = noteService.getByNoteId(noteId);
        if(share!=null){
            result.setData(share);
            result.setStatus(1);
            result.setMsg("笔记已存在");
            return result;
        }else if (note==null){
            result.setMsg("笔记不存在");
            result.setStatus(2);
            return  result;
        }
        //没分享过再添加到分享
        Share s = new Share();
        s.setNoteId(noteId);
        s.setShareId(NoteUtil.createId());
        s.setShareTittle(note.getNoteTittle());
        s.setShareBody(note.getNoteBody());
        noteService.addShare(s);
        result.setStatus(0);
        result.setData(s);
        result.setMsg("分享成功");
        return result;
    }

    @RequestMapping("/search.do")
    public NoteResult getByTittle(String noteTittle){
        List<Share> notes = noteService.getByTittle(noteTittle);
        NoteResult result = new NoteResult();
        result.setData(notes);
        result.setStatus(0);
        result.setMsg("查询成功");
        return result;
    }
    @RequestMapping("/loadShare.do")
    public NoteResult getByShareId(String shareId){
        NoteResult result = new NoteResult();
        Share share = noteService.getById(shareId);
        result.setData(share);
        result.setStatus(0);
        result.setMsg("查询成功");
        return  result;
    }

    @RequestMapping("/move.do")
    public NoteResult moveNote(String noteBookId,String noteId){
        NoteResult result = new NoteResult();
        int count = noteService.moveNote(noteBookId, noteId);
        if(count!=0){
            result.setStatus(0);
            result.setMsg("移动成功");
        }else {
            result.setStatus(1);
            result.setMsg("移动失败");
        }
        return result;
    }
}
